package primeNums;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Prime {
    static CountDownLatch count;
    public static void main(String[] args) throws InterruptedException {

        ConsoleReader consoleIn = new ConsoleReader();

        long start = System.nanoTime();

        ExecutorService exs = Executors.newFixedThreadPool(4);

        boolean[] primeArray = new boolean[consoleIn.number+1];
        primeArray[0] = true;
        primeArray[1] = true;

        count = new CountDownLatch(consoleIn.threads);
        int part = consoleIn.number / (consoleIn.threads*2);

        for (int i = 1; i <= consoleIn.threads; i++){
            if (i==1) exs.execute(new Task(2, primeArray));
            exs.execute(new Task(part * (i - 1), primeArray));
        }



//        for (int i = 0; i < primeArray.length; i++) {
//            if (!primeArray[i]) {
//                for (int j = 2 * i; j < primeArray.length; j += i) {
//                    primeArray[j] = true;
//                }
//            }
//        }
//        int k = 0;
//        for (int i=0;i<primeArray.length;i++) {
//            if (!primeArray[i]) {
//                System.out.println(i+" is prime");
//                k++;
//            }
//        }
//        System.out.println(k);
        count.await();

        long end = System.nanoTime();
        System.out.println("\n" + (end - start)/1000000);

        exs.shutdown();
    }
}

